<!-- main -->
<link rel="stylesheet" type="text/css" media="all" href="<?=$this->data_url?>index_stor.css" />

<body>
<!-- Контейнер -->
<table width="100%">
	<tr>
		<td class="row_title" width="150px"><?=$mvc->lang->tr('Table name');?></td>
		<td>
			<input type="text" id="stor_name" value="<?=$this->table->name?>"/>
		</td>
	</tr>
	<tr>
		<td>
			<span class="row_title"><?=$mvc->lang->tr('Comment');?></span>
		</td>
		<td>	<input type="text" id="stor_desc" value="<?=$this->table->comment ?>" />
		</td>
	</tr>

	<tr>
		<td valign="top" colspan="2">
			<div class="block_title"><?=$mvc->lang->tr('Fields editor');?></div>
			<table border="1" id="datatable" class="datatable ---fields_table" width="100%">
				<tr>
					<td class="row_title"><?=$mvc->lang->tr('Name');?></td>
					<td class="row_title"><?=$mvc->lang->tr('Type');?></td>
					<td class="row_title"><?=$mvc->lang->tr('Length');?></td>
					<td class="row_title"><?=$mvc->lang->tr('Comment');?></td>
					<td>&nbsp;</td>
				</tr>
				<tr style="background: #eee">
					<td><input type="text" name="name" id="field_name" /></td>
					<td>
						<select name="type" id="field_type">
							<option value="int">int</option>
							<option value="float">float</option>
							<option value="char">char</option>
							<option value="varchar">varchar</option>
							<option value="date">date</option>
							<option value="time">time</option>
							<option value="datetime">datetime</option>
							<option value="text">text</option>
						</select>
					</td>
					<td><input type="text" name="len" id="field_len" /></td>
					<td><input type="text" name="comment" id="field_comment" /></td>
					<td width="1%"><a class="btn_set" title="<?=$mvc->lang->tr('Set');?>" onClick="setRow()">S</a></td>
				</tr>
			</table>
		</td>
	</tr>

	<tr>
		<td colspan="2" align="center">
			<input type="button" value="<?=$mvc->lang->tr('Save');?>" onClick="createStorage()" />
			<input type="button" value="<?=$mvc->lang->tr('Cancel');?>" onClick="document.location='<?=$this->base_url?>'" />
		</td>
	</tr>
</table>

<script type="text/javascript">
	str = new Array();
	<?
		foreach($this->table->fields as $k => $v)
		{
			echo "str['".$k."'] = {";
			echo "name: '".$v['name']."'";
			echo ", type: '".$v['type']."'";
			echo ", len: '".$v['len']."'";
			echo ", comment: '".$v['comment']."'};";
		}
	?>

	baseTableCode = jQuery('#stor_container').html();

	function updateTable()
	{
		jQuery("#datatable tr[alt]").remove();

		for(var k in str)
		{
			var row = '<tr alt="' + k + '" >';
			//row += ' onmouseover="this.style.background=\'#eee\'"';
			//row += ' onmouseout="this.style.background=\'\'">';
			row += '<td><a href="javascript:getRow(\'' + str[k].name + '\')" class="plainlink">' + k + '</a></td>';
			row += '<td>' + str[k].type + '</td>';
			row += '<td>' + str[k].len + '</td>';
			row += '<td>' + str[k].comment + '</td>';
			row += '<td><a href="javascript:delRow(\'' + k + '\')" class="btn_del"><?=$mvc->lang->tr("Delete")?></a></td>';
			row += '</tr>';
			jQuery("#datatable").append(row);
		}
		jQuery("#datatable .btn_del").button({icons:{primary:'ui-icon-trash'}, text:false});
		// Подсветка строки таблицы.
		jQuery('.datatable tbody tr').mouseover( function() {jQuery(this).addClass('hover'); }) ;
		jQuery('.datatable tbody tr').mouseout( function() {jQuery(this).removeClass('hover'); }) ;
	}

	function getRow(name)
	{
		jQuery('#field_name').val(str[name]['name']);
		jQuery('#field_type').val(str[name]['type']);
		jQuery('#field_len').val(str[name]['len']);
		jQuery('#field_comment').val(str[name]['comment']);
	}

	function setRow()
	{
		var name = jQuery('#field_name').val().trim().toLowerCase();
		if (name != '')
		{
			str[name] = {
				name: jQuery('#field_name').val(),
				type: jQuery('#field_type').val(),
				len: jQuery('#field_len').val(),
				comment: jQuery('#field_comment').val()
			}
		};
		updateTable();
	}

	function delRow(field_name)
	{
		var newStr = Array();
		for (var k in str)
			if (field_name != k) newStr[k] = str[k];
		str = newStr;

		updateTable();
	}

	function addValue(form, name, value)
	{
		form.append('<textarea name="' + name + '">' + value + '</textarea>');
	}

	function createStorage()
	{
		var form = jQuery(document.createElement('form'));
		form.css({'display':'none'});

		form.attr('action', '<?=$this->base_url?>save?name=' + jQuery('#stor_name').val());
		form.attr('method', 'POST');

		addValue(form, 'name', jQuery('#stor_name').val());
		addValue(form, 'desc', jQuery('#stor_desc').val());

		var i = 0;
		for(var k in str)
		{
			addValue(form, 'stor_f' + i + 'N', str[k]['name']);
			addValue(form, 'stor_f' + i + 'T', str[k]['type']);
			addValue(form, 'stor_f' + i + 'L', str[k]['len']);
			addValue(form, 'stor_f' + i + 'C', str[k]['comment']);
			i++
		}
		addValue(form, 'stor_fcount', i);
		
		jQuery(document.body).append(form);
		form.submit();
	}

	updateTable();
</script>

<script>
	jQuery('.btn_add').button({icons:{primary:'ui-icon-circle-plus'}, text:false});
	jQuery('.btn_edit').button({icons:{primary:'ui-icon-pencil'}, text:false});
	jQuery('.btn_del').button({icons:{primary:'ui-icon-trash'}, text:false});
	jQuery('.btn_set').button({icons:{primary:'ui-icon-circle-check'}, text:false});
	// Подсветка строки таблицы.
	jQuery('.datatable tbody tr').mouseover( function() {jQuery(this).addClass('hover'); }) ;
	jQuery('.datatable tbody tr').mouseout( function() {jQuery(this).removeClass('hover'); }) ;
</script>




</body>
</html>